/**
 * @program: LeetCode
 * @description: LeetCode : 303. 区域和检索 - 数组不可变
 * @author: WXY
 * @create: 2023-01-08 21:11
 * @Version 1.0
 **/
public class Num303_NumArray {
    static class NumArray1 {
        int[] arr;

        public NumArray1(int[] nums) {
            arr = nums.clone();
        }

        public static void main(String[] args) {
            int[] arr = {-2, 0, 3, -5, 2, -1};
            NumArray1 numArray1 = new NumArray1(arr);
            int i = numArray1.sumRange(2, 5);
            System.out.println(i);

        }

        public int sumRange(int left, int right) {
            int ans = 0;
            for (int i = left; i <= right; i++) {
                ans += arr[i];
            }
            return ans;
        }
    }


    class NumArray {
        int[] arr;

        public NumArray(int[] nums) {
            arr = new int[nums.length + 1];
            arr[0] = nums[0];
            for (int i = 1; i < nums.length; i++) {
                arr[i] = arr[i - 1] + nums[i];
            }
        }

        public int sumRange(int left, int right) {
            if (left == 0) return arr[right];
            return arr[right] - arr[left];
        }
    }


}
